//
//  Meds.m - a collection of NDCItems
//  iMed
//
//  Created by Barry Press on 8/15/09.
//  Copyright 2009 Barry Press. All rights reserved.
//

#import "Meds.h"
#import "MyDocument.h"

@implementation Meds

// =============================================================================
// ---------------------------------------------- initialize -------------------
+ (void)initialize
{
}

// ---------------------------------------------- addObject --------------------
- (void)addObject:(NDCitem*)anObject
{
	NSAssert(meds != NULL, @"NULL array pointer in Meds:addObject");
	[meds addObject:anObject];
}

// ---------------------------------------------- count ------------------------
- (int)count
{
	NSAssert(meds != NULL, @"NULL array pointer in Meds:count");
	return [meds count];
}

// ---------------------------------------------- objectAtIndex ----------------
- (NDCitem*)objectAtIndex:(int)index
{
	NSAssert(meds != NULL, @"NULL array pointer in Meds:objectAtIndex");
	return [meds objectAtIndex:index];
}

// ---------------------------------------------- objectWithName ---------------
- (NDCitem*)objectWithName:(NSString*)aName
{
	NDCitem* ndcValue = nil;
	NSAssert(meds != NULL, @"NULL array pointer in Meds:objectWithName");
	
	for (int ix = 0; ix < [meds count]; ++ix) {
		NDCitem *ndc = [meds objectAtIndex:ix];
		NSAssert([ndc validateObject], @"Bad object in Meds array");
		if ([[ndc objectAtKey:@"Name"] hasPrefix:aName]) {
			ndcValue = ndc;
		}
	}
	return ndcValue;
}

// ---------------------------------------------- removeAllObjects -------------
- (void)removeAllObjects
{
	NSAssert(meds != NULL, @"NULL array pointer in Meds:removeAllObjects");
	[meds removeAllObjects];
}

// ---------------------------------------------- removeObject -----------------
- (void)removeObject:(NDCitem*)anObject
{
	NSAssert(meds != NULL, @"NULL array pointer in Meds");
	[meds removeObject:anObject];
}

#pragma mark Archiving methods

// ---------------------------------------------- dealloc ----------------------
- (void)dealloc
{
	NSAssert(meds != NULL, @"NULL array pointer in Meds:removeObject");
	[meds release];
	meds = NULL;
	[super dealloc];
}

// ---------------------------------------------- init -------------------------
- (id)init
{
	[super init];
	meds = [[[NSMutableArray alloc] init] retain];
	NSAssert(meds != NULL, @"NULL array pointer in Meds:init");
	return self;
}

// ---------------------------------------------- encodeWithCoder --------------
- (void)encodeWithCoder:(NSCoder *)coder
{
	NSAssert(meds != NULL, @"NULL array pointer in Meds:encodeWithCoder");
	[coder encodeObject:meds forKey:@"MedsArray"];
}

// ---------------------------------------------- initWithCoder ----------------
- (id)initWithCoder:(NSCoder *)coder
{
	[super init];
	meds = [[coder decodeObjectForKey:@"MedsArray"] retain];
	return self;
}

// ---------------------------------------------- validateObject ---------------
- (BOOL)validateObject
{
	BOOL bOk;
	
	bOk	= (meds != NULL) && ([meds isKindOfClass:[NSMutableArray class]]);
	for (int ix = 0; bOk && ix < [meds count]; ++ix) {
		bOk = [[meds objectAtIndex:ix] validateObject];
	}
	return bOk;
}

@end
